//
//  PBPositionCell.m
//  PBHomeDemo
//
//  Created by Playboy on 2018/1/31.
//  Copyright © 2018年 tmkj. All rights reserved.
//

#import "PBPositionCell.h"
#import "MenuListView.h"

@interface PBPositionCell ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *firstTableView;
@property (strong, nonatomic) IBOutlet UITableView *secTableView;
@property (nonatomic, strong) NSArray   *dataSource;
@property (nonatomic, strong) NSMutableArray *desArray;
@property (nonatomic, strong) NSMutableArray *desIdArray;
@property (nonatomic, strong) NSIndexPath *firstSelectIndexPath;
@end

@implementation PBPositionCell

- (NSMutableArray *)desArray {
    if (!_desArray) {
        _desArray = [[NSMutableArray alloc] init];
    }
    return _desArray;
}
- (NSMutableArray *)desIdArray {
    if (!_desIdArray) {
        _desIdArray = [[NSMutableArray alloc] init];
    }
    return _desIdArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.firstTableView.tableFooterView = [[UIView alloc] init];
    self.secTableView.tableFooterView = [[UIView alloc] init];
    self.firstTableView.delegate = self;
    self.firstTableView.dataSource = self;
    self.secTableView.delegate = self;
    self.secTableView.dataSource = self;
    self.dataSource = @[@"附近",@"区域",@"地铁",@"重置"];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.firstTableView) {
        return 4;
    }
    return self.desArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListCell *cell = [ListCell cellWithTableView:tableView];
    if (tableView == self.firstTableView) {
        cell.title = self.dataSource[indexPath.row];
    }else {
        cell.title = self.desArray[indexPath.row];
    }
//    cell.selected = [self.dataSource[indexPath.row] isEqualToString:_currenTitle];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.firstTableView) {
        self.firstSelectIndexPath = indexPath;
        if (indexPath.row == 0) {
            self.desArray = nil;
            if (self.delegate && [self.delegate respondsToSelector:@selector(fujinButtonClick)]) {
                [self.delegate fujinButtonClick];
            }
        }else if (indexPath.row == 1) {
            NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId};
            [TMNetwork requestGetWithParam:param url:@"/position/county" callback:^(TMBaseReqModel *model) {
                if (model.code == 200) {
                    [self.desArray removeAllObjects];
                    [self.desIdArray removeAllObjects];
                    for (NSDictionary *dic in model.data) {
                        [self.desArray addObject:dic[@"countyName"]];
                        [self.desIdArray addObject:dic[@"countyId"]];
                    }
                    [self.secTableView reloadData];
                }
            }];
        }else if (indexPath.row == 2) {
            NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId};
            [TMNetwork requestGetWithParam:param url:@"/position/route" callback:^(TMBaseReqModel *model) {
                if (model.code == 200) {
                    [self.desArray removeAllObjects];
                    [self.desIdArray removeAllObjects];
                    for (NSDictionary *dic in model.data) {
                        [self.desArray addObject:dic[@"name"]];
                        [self.desIdArray addObject:dic[@"id"]];
                    }
                    [self.secTableView reloadData];
                }
            }];
        }else if (indexPath.row == 3) {
            self.desArray = nil;
            if (self.delegate && [self.delegate respondsToSelector:@selector(resetPositionButtonClick)]) {
                [self.delegate resetPositionButtonClick];
            }
        }
        [self.secTableView reloadData];
    }else {
        if (self.firstSelectIndexPath.row == 1) {
            // 区域
            if (self.delegate && [self.delegate respondsToSelector:@selector(zeroButtonClickWithId:)]) {
                [self.delegate zeroButtonClickWithId:self.desIdArray[indexPath.row]];
            }
            
        }else if (self.firstSelectIndexPath.row == 2) {
            // 地铁
            if (self.delegate && [self.delegate respondsToSelector:@selector(trainButtonClickWithId:)]) {
                [self.delegate trainButtonClickWithId:self.desIdArray[indexPath.row]];
            }
        }
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
